package LecoLi.Item02;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.GridView;

import com.demo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;

import LecoLi.Util.Item02.Bean;
import LecoLi.Util.Item02.GridAdapter;

/**
 * Created by Le Co on 2017/5/11.
 */

public class GradleLayoutFragment extends Fragment {
    public static final String TAG="GradleLayoutFragment";
    private View view;
    private GridView gridLayout;
    private String [] names ;
    private int [] dates = new int[6];
    private GridAdapter adapter;
    private TimerTask task;
    private Random random;
    private android.os.Handler handler = new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.arg1==1){
                adapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.leco_item_02,null);
        names = getResources().getStringArray(R.array.grid_list);
        gridLayout = $(R.id.leco_item_02_grad);

        adapter = new GridAdapter(getActivity(),names,dates);
        gridLayout.setAdapter(adapter);
        task = new TimerTask() {
            @Override
            public void run() {
                for (int i = 0; i < 6; i++) {
                    dates[i] = (int) (Math.random()*100);
                }
               Message message = new Message();
                message.arg1=1;
                handler.sendMessage(message);
            }
        };
        new Timer().schedule(task,0,5000);
        adapter.notifyDataSetChanged();
        return view;


    }


    @Override
    public void onStart() {
        super.onStart();

    }

    private <T extends View> T $(int i){
        return (T) view.findViewById(i);
    }





}
